
package com.hlkj.pay.app.merchant.ext.impl.huifu;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantAccessExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.access.*;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.huifu.dto.HuiFuConfig;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.HuiFuMerchantAccessConfigHandler;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.HuiFuMerchantAccessInfoHandler;
import com.hlkj.pay.app.merchant.ext.impl.huifu.handler.params.HuiFuMerchantAccessCommonParamsHandle;
import com.hlkj.pay.app.merchant.ext.impl.huifu.utils.HuiFuCallBackParamsParseUtil;
import com.hlkj.pay.dto.merchant.AccessChannelParamsDto;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.util.HttpRequestUtil;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;
import com.huifu.bspay.sdk.opps.core.request.V2MerchantBasicdataEntRequest;
import com.huifu.bspay.sdk.opps.core.request.V2MerchantBasicdataIndvRequest;
import com.huifu.bspay.sdk.opps.core.request.V2MerchantBusiOpenRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 汇付进件操作
 * 
 * @author HlpayTeam
 * @date 2024/09/26 10:25
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_HUI_FU)
public class HuiFuMerchantAccessExtPt extends HuiFuMerchantAccessCommonParamsHandle implements ChannelMerchantAccessExtPt {

    private final HuiFuMerchantAccessInfoHandler huiFuMerchantAccessInfoHandler;

    private final HuiFuMerchantAccessConfigHandler huiFuMerchantAccessConfigHandler;

    @Override
    public void channelConfigCheck(ChannelMerchantAccessCommonContext channelMerchantAccessCommonContext) {
        CommonResult<HuiFuConfig> commonResult = huiFuConfig(channelMerchantAccessCommonContext.getProviderChannelConfig(), channelMerchantAccessCommonContext.isTest());
        if (!commonResult.isSuccess()) {
            channelMerchantAccessCommonContext.setRemark(commonResult.getMsg());
            channelMerchantAccessCommonContext.setProcessStatus(false);
        }
    }
    @Override
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("汇付渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        huiFuMerchantAccessInfoHandler.merchantAccess(channelMerchantAccessContext);
    }

    @Override
    public String requestUrl(MerchantAccessDto merchantAccessDto) {
        MerchantEnum.ACCESS_STATUS accessStatus = MerchantEnum.ACCESS_STATUS.from(merchantAccessDto.getMerchantAccessInfo().getStatus());
        switch (accessStatus) {
            case WAIT_SETTING:
                return new V2MerchantBusiOpenRequest().getFunctionCode().getCode();
            default:
                switch (MerchantEnum.MERCHANT_TYPE.from(merchantAccessDto.getMerchantAccessInfo().getType())) {
                    case PERSONAL:
                        return new V2MerchantBasicdataIndvRequest().getFunctionCode().getCode();
                    default:
                        return new V2MerchantBasicdataEntRequest().getFunctionCode().getCode();
                }
        }
    }

    @Override
    public String channelParams(AccessChannelParamsDto accessChannelParamsDto) {
        HuiFuAppChannelParams params = new HuiFuAppChannelParams();
        params.setHuiFuId(accessChannelParamsDto.getSubMchId());
        return JsonUtils.toJsonStringNotNull(params);
    }

    @Override
    public void merchantAccessQuery(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {
        huiFuMerchantAccessInfoHandler.merchantAccessQuery(channelMerchantAccessQueryContext);
    }

    @Override
    public void merchantSign(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
        // 汇付没有签约流程
        // channelMerchantAccessSignContext.setProcessStatus(CommonEnum.YES_NO_TYPE.YES);
        // 汇付没有签约 直接进入待配置
        channelMerchantAccessSignContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
    }

    @Override
    public void merchantSignQuery(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
        log.info("汇付渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessSignContext));
        huiFuMerchantAccessInfoHandler.merchantSignQuery(channelMerchantAccessSignContext);
    }

    @Override
    public void merchantBusinessOpen(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // huiFuMerchantAccessInfoHandler.merchantBusinessOpen(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_CONFIG);
        channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
    }

    @Override
    public void wechatConfigInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // huiFuMerchantAccessConfigHandler.wechatAuth(channelMerchantBusinessContext);
        // huiFuMerchantAccessConfigHandler.subAuthQuery(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_AUTH);
    }

    @Override
    public void wechatConfigAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        huiFuMerchantAccessConfigHandler.wechatConfig(channelMerchantBusinessContext);
    }

    @Override
    public void wechatConfigQuery(ChannelMerchantAccessAuthQueryContext merchantBusinessContext) {
        huiFuMerchantAccessConfigHandler.wechatConfigQuery(merchantBusinessContext);
    }

    @Override
    public void wechatAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // huiFuMerchantAccessConfigHandler.wechatAuth(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_AUTHING);
    }

    @Override
    public boolean authConfigQueryType() {
        return true;
    }

    @Override
    public void wechatAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantBusinessContext) {
        huiFuMerchantAccessConfigHandler.subAuthQuery(channelMerchantBusinessContext, "W");
        huiFuMerchantAccessConfigHandler.wechatAuthQuery(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setMerchantAccessAuthList(channelMerchantBusinessContext.getMerchantAccessAuthDbList());
    }

    @Override
    public void alipayAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // huiFuMerchantAccessConfigHandler.alipayAuth(channelMerchantBusinessContext);
        // huiFuMerchantAccessConfigHandler.alipayAuth(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTHING);
    }

    @Override
    public void alipayAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantBusinessContext) {
        huiFuMerchantAccessConfigHandler.subAuthQuery(channelMerchantBusinessContext, "A");
        huiFuMerchantAccessConfigHandler.alipayAuthQuery(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setMerchantAccessAuthList(channelMerchantBusinessContext.getMerchantAccessAuthDbList());
    }

    @Override
    public MerchantAccessCallBackDto accessCallBackParamsParse(HttpServletRequest callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        String accessCallBody = HttpRequestUtil.requestBodyParams(callBackBody);
        log.info("汇付进件结果回调 accessCallBody:{}", accessCallBody);
        return HuiFuCallBackParamsParseUtil.accessCallBackParamsParse(accessCallBody, providerChannelConfigDO);

    }

    @Override
    public CommonResult<String> accessCallbackParse(String callBackBody, List<ProviderChannelConfigDO> channelConfigDOList) {
        return null;
    }

    @Override
    public MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return HuiFuCallBackParamsParseUtil.businessCallBackParamsParse(callBackBody);
    }

    @Override
    public boolean verify(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HuiFuConfig huiFuConfig = JsonUtils.parseObject(channelConfig, HuiFuConfig.class);
        return HuiFuCallBackParamsParseUtil.verify(huiFuConfig, callBackBody);
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        if (commonResult.isSuccess()) {
            return "RECV_ORD_ID_" + commonResult.getData();
        }
        else {
            return "0";
        }
    }

    @Override
    public void termAdd(ChannelMerchantTermContext channelMerchantTermContext) {

    }

    @Override
    public void reloadMerchantAuth(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public void productUpdate(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public Integer queryMerAuthStatus(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        return null;
    }

    @Override
    public MerchantProductQueryResp productQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        return null;
    }

    @Override
    public MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

}
